package com.zhou.util;

import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.GifDecoder;
import net.coobird.thumbnailator.Thumbnails;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * @author lang.zhou
 * @date 2022/4/28 14:33
 */
public class asd {
    public static void main(String[] args) throws Exception{
        /*byte[] d = FileUtils.readFileToByteArray(new File("C:\\Users\\zhou\\Desktop\\2.jpeg"));
        byte[] compress = compressImg(d, 0.8f);
        FileUtils.writeByteArrayToFile(new File("C:\\Users\\zhou\\Desktop\\3.jpeg"),compress);*/

        t1();
        /*byte[] d = FileUtils.readFileToByteArray(new File("C:\\Users\\zhou\\Desktop\\1.gif"));
        byte[] compress = compressGif(d,0.2f);
        FileUtils.writeByteArrayToFile(new File("C:\\Users\\zhou\\Desktop\\2.gif"),compress);
        System.out.println();*/
    }

    public static byte[] compressGif(byte[] data,float rate) throws Exception{
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GifDecoder decoder = new GifDecoder();
        decoder.read(new ByteArrayInputStream(data));
        int width = (int) (decoder.getImage().getWidth()* rate);
        int height = (int) (decoder.getImage().getHeight()* rate);

        AnimatedGifEncoder e = new AnimatedGifEncoder();
        // 设置生成图片大小
        e.setSize(width, height);
        //保存到数组
        e.start(out);
        //重复次数 0表示无限重复 默认不重复
        e.setRepeat(0);
        int cnt = decoder.getFrameCount();

        for (int i = 0; i < cnt; i++) {
            BufferedImage frame = decoder.getFrame(i);
            BufferedImage image = compressFrame(frame , rate);
            e.addFrame(image);
            e.setDelay(decoder.getDelay(i));
        }
        e.finish();
        return out.toByteArray();
    }
    public static BufferedImage compressFrame(BufferedImage frame, float rate) throws Exception{
        ByteArrayOutputStream o1 = new ByteArrayOutputStream();
        //第一次压缩后可能会变大，在变大的基础上重新压缩一遍
        BufferedImage jpg = Thumbnails.of(frame).scale(1f).outputFormat("jpg").asBufferedImage();
        return Thumbnails.of(jpg).scale(rate).outputFormat("jpg").asBufferedImage();
    }
    public static byte[] compressImg(byte[] data,float q) throws Exception{
        ByteArrayOutputStream o1 = new ByteArrayOutputStream();
        ByteArrayOutputStream o2 = new ByteArrayOutputStream();
        //第一次压缩后可能会变大，在变大的基础上重新压缩一遍
        Thumbnails.of(new ByteArrayInputStream(data)).scale(1f).outputFormat("jpg").toOutputStream(o1);
        byte[] bytes = o1.toByteArray();
        float nq = data.length * q/bytes.length;
        Thumbnails.of(new ByteArrayInputStream(bytes)).scale(nq).outputQuality(nq).outputFormat("jpg").toOutputStream(o2);
        byte[] array = o2.toByteArray();
        System.out.println(String.format("压缩前：%s",data.length));
        System.out.println(String.format("压缩一次后：%s",bytes.length));
        System.out.println(String.format("重新计算后的质量：%s",nq));
        System.out.println(String.format("压缩两次后：%s",array.length));
        System.out.println(String.format("实际压缩质量：%s",(array.length+0.00f)/data.length));
        return array;
    }


    public static void t1() throws Exception{
        for (int i = 1939; i <= 1965; i++) {
            HttpsUtil.doDelete("http://120.48.38.206:9200/emoticon/_doc/"+i,null);
        }
    }

}
